package com.sxz.sbs.module.order;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sxz.sbs.R;
import com.sxz.sbs.applibrary.state.StateLayout;
import com.sxz.sbs.base.BaseFragment;
import com.sxz.sbs.http.retrofit.HttpParamMap;
import com.sxz.sbs.module.order.adapter.OrderListAdapter;
import com.sxz.sbs.module.order.mode.IntentOrder;
import com.sxz.sbs.module.order.mode.OrderhandleBean;
import com.sxz.sbs.module.order.presenter.OrderListPresenter;
import com.sxz.sbs.module.order.view.OrderListView;
import com.sxz.sbs.widget.PhoneDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;

/**
 * @author hp
 *
 */
public class OrderListFragment extends BaseFragment<OrderListPresenter> implements OrderListView {

    private int mType;
    @BindView(R.id.stateLayout)
    StateLayout stateLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private OrderListAdapter allHousesAdapter;
    private List<OrderhandleBean.ListBean> list=new ArrayList<>();
    private int currentPage = 1;
    private static String keyWords = "";
    private static final String NEW_INSTANCE = "order_fragment";
    public static OrderListFragment newInstance(int type) {
        Bundle args = new Bundle();
        OrderListFragment fragment = new OrderListFragment();
        args.putInt(NEW_INSTANCE,type);
        fragment.setArguments(args);
        return fragment;
    }
    public  static void  setKeyWords(String key) {
        keyWords = key;
    }
    public void refresh() {
        currentPage = 1;
        getOrderListData();
    }

    @Override
    public void onResume() {
        super.onResume();
        currentPage = 1;
        getOrderListData();
    }

    @Override
    protected void init() {
        mType = getArguments().getInt(NEW_INSTANCE);
        refreshLayout.setColorSchemeResources(R.color.color_main);
        refreshLayout.setRefreshing(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        allHousesAdapter=new OrderListAdapter(list);
        recyclerView.setAdapter(allHousesAdapter);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                currentPage = 1;
                getOrderListData();
            }
        });
        allHousesAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                currentPage ++;
                getOrderListData();
            }
        },recyclerView);

        allHousesAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent=new Intent(getActivity(),OrderDetailActivity.class);
                intent.putExtra("order_id",list.get(position).getId());
                startActivity(intent);
            }
        });
        allHousesAdapter.setOnOrderClickListener(new OrderListAdapter.OnOrderClickListener() {
            @Override
            public void onAllotOrder(OrderhandleBean.ListBean item) {
                IntentOrder intentOrder = new IntentOrder();
                intentOrder.setOrderId(item.getId());
                intentOrder.setParkingId(item.getParkingId());
                startActivity(new Intent(getActivity(),AllotOrderActivity.class)
                        .putExtra("intent_order",intentOrder));
            }

            @Override
            public void onCallOrder(String phone) {
                PhoneDialog.callPhone(getActivity(),phone);
            }

            @Override
            public void onModifyOrder(OrderhandleBean.ListBean item) {
                IntentOrder intentOrder1 = new IntentOrder();
                intentOrder1.setOrderId(item.getId());
                intentOrder1.setIsByStage(item.getIsByStage());
                intentOrder1.setPrice(item.getDealPrice() + "");
                intentOrder1.setStatusInt(item.getStatus());
                intentOrder1.setRemark(item.getRemark());
                startActivity(new Intent(getActivity(),OrderModifyActivity.class)
                        .putExtra("intent_order",intentOrder1));
            }
        });
        stateLayout.setOnRetryListener(new StateLayout.OnRetryListener() {
            @Override
            public void onRetry(int state) {
                getOrderListData();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_orderlist;
    }


    private void getOrderListData(){
        refreshLayout.setRefreshing(true);
        HashMap<String, Object> map = HttpParamMap.getParamMap();
        map.put("status", mType);
        map.put("pageNumber",currentPage);
        map.put("pageSize", 10);
        map.put("keyword", keyWords);
        mPresenter.getOrderList(map);
    }

    @Override
    protected OrderListPresenter createPresenter() {
        return new OrderListPresenter();
    }

    @Override
    public void onDestroyView() {
        currentPage = 1;
        list.clear();
        super.onDestroyView();

    }

    @Override
    public void onOrderListSuccess(OrderhandleBean data) {
        if (mIsLive && data != null) {
            refreshLayout.setRefreshing(false);
            if (data.getList() != null && data.getList().size() >0) {
                stateLayout.showContent();
                if (currentPage == 1) {
                    list.clear();
                }
                list.addAll(data.getList());
                allHousesAdapter.notifyDataSetChanged();
                allHousesAdapter.loadMoreComplete();
            } else {
                if (currentPage == 1) {
                    list.clear();
                    allHousesAdapter.notifyDataSetChanged();
                    //  数据为空
                    stateLayout.showEmpty();
                } else {
                    allHousesAdapter.loadMoreEnd();
                    allHousesAdapter.notifyDataSetChanged();
                }
            }
        }
    }
}